package cn.suchan.jianzhi.q18_mirror;

import java.util.Stack;

/**
 * 知识点：二叉树的镜像
 * 题目描述
 * 操作给定的二叉树，将其变换为源二叉树的镜像。
 * 输入描述:
 * 二叉树的镜像定义：
 * #      源二叉树:
 * #         8
 * #       /  \
 * #      6   10
 * #     / \  / \
 * #    5  7 9 11
 * #     镜像二叉树:
 * #         8
 * #       /  \
 * #      10   6
 * #     / \  / \
 * #    11 9 7  5
 *
 * @author suchan
 * @date 2019/05/27
 */
public class Solution {

    /**
     * 递归解决
     *
     * @param root
     */
    public void Mirror(TreeNode root) {
        if (root != null
                && (root.left != null || root.right != null)) {
            TreeNode temp = root.left;
            root.left = root.right;
            root.right = temp;
            Mirror(root.left);
            Mirror(root.right);
        }
    }

    public static void main(String[] args) {
        TreeNode node21 = new TreeNode(21);
        TreeNode node22 = new TreeNode(22);
        TreeNode node23 = new TreeNode(23);
        TreeNode node24 = new TreeNode(24);
        TreeNode node25 = new TreeNode(25);
        TreeNode node26 = new TreeNode(26);
        TreeNode node27 = new TreeNode(27);
        TreeNode node28 = new TreeNode(28);
        TreeNode node29 = new TreeNode(29);
        TreeNode node30 = new TreeNode(30);

        node21.left = node22;
        node21.right = node23;
        node22.left = node24;
        node22.right = node25;
        node23.left = node26;
        node23.right = node27;
        node24.left = node28;
        node24.right = node29;
        node25.left = node30;

        Solution solution = new Solution();
        System.out.println("===========before===========");
        System.out.println(node21);
        solution.Mirror(node21);
        System.out.println("===========after============");
        System.out.println(node21);
    }
}
